package com.cictec.middleware.gps.engine.position.handler.alarms;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.cictec.middleware.commons.utils.Constants;
import com.cictec.middleware.commons.utils.DateUtils;
import com.cictec.middleware.commons.utils.NumberUtil;
import com.cictec.middleware.commons.utils.PMSUtils;
import com.cictec.middleware.commons.utils.gps.GpsUtils;
import com.cictec.middleware.core.entity.WarnMsg;
import com.cictec.middleware.gps.engine.BusInstance;
import com.cictec.middleware.gps.engine.TimingPoint;
import com.cictec.middleware.gps.engine.exception.BusPositionWarnHandlerException;
import com.cictec.middleware.gps.engine.position.handler.BusPositionAlarmsHandler;
import com.cictec.middleware.gps.service.WarnMsgService;

/**
 * 车辆偏线报警
 * @author ql
 * @date 2017年9月26日
 */
@Component
public class BusDeviateWarnHandler implements WarnHandler {

	private Logger logger = LoggerFactory.getLogger(BusPositionAlarmsHandler.class);
	
	@Autowired
	private WarnMsgService warnMsgService;
	
	
	@Override
	public String getWarnType() {
		return Constants.VAL_BUS_STATUS_7;
	}
	
	
	@Override
	public void onHandler(BusInstance busInstance) throws BusPositionWarnHandlerException {
	
		logger.debug("【偏线报警信息处理】");
		WarnMsg warnMsg = busInstance.getWarnsByWarnType(Constants.VAL_BUS_STATUS_7);
		
//		if(true){	// 测试偏线报警
		if(busInstance.isInDeviate()){
			if(warnMsg == null || !PMSUtils.isEmpty(warnMsg.getWarnClosedTime())){
				warnMsg = new WarnMsg();
				warnMsg.setWarnBusUuid(busInstance.getBusUuid());
				warnMsg.setWarnDevUuid(busInstance.getDevUuid());

				if (!PMSUtils.isEmpty(busInstance.getDriverUuid())) {
					warnMsg.setWarnDrvUuid(busInstance.getDriverUuid());
				}
				warnMsg.setWarnLineUuid(busInstance.getLineUuid());
				warnMsg.setWarnSpeed(busInstance.getSpeed());
				warnMsg.setWarnStartLat(busInstance.getLatitude());
				warnMsg.setWarnStartLng(busInstance.getLongitude());
				warnMsg.setWarnStartTime(DateUtils.formatDateTimes(busInstance.getTime()));
				warnMsg.setWarnType(Constants.VAL_BUS_STATUS_7);
				warnMsg.setWarnStorageTime(DateUtils.formatDateTimes(new Date()));
				
				busInstance.setWarns(Constants.VAL_BUS_STATUS_7, warnMsg);
				logger.error("保存报警{}", JSON.toJSONString(warnMsg));
				warnMsgService.saveWarn(warnMsg);
			}else{
				// 计算偏线距离
				try {
					TimingPoint pre = busInstance.getTimingPoint(-1);
					Double leng = new Double(0);
					if(pre != null){
						leng = GpsUtils.getDistance(
								pre.getLatitude(),
								pre.getLongitude(),
								busInstance.getLatitude(),
								busInstance.getLongitude());
					}
					String warnMileages = warnMsg.getWarnMiles();
					leng = NumberUtil.add(PMSUtils.isEmpty(warnMileages) ? 0 : Double.parseDouble(warnMileages), leng);
					warnMsg.setWarnMiles(leng.toString());
				} catch (NumberFormatException e) {
					e.printStackTrace();
				}
			}
		}else{
			if (warnMsg != null && PMSUtils.isEmpty(warnMsg.getWarnEndTime())) {
				warnMsg.setWarnDuration(PMSUtils.isNull(DateUtils.secondInterval(busInstance.getTime(), DateUtils.parseDateTimes(warnMsg.getWarnStartTime()))));
				warnMsg.setWarnEndTime(DateUtils.formatDateTimes(busInstance.getTime()));
				warnMsg.setWarnEndLat(busInstance.getLatitude());
				warnMsg.setWarnEndLng(busInstance.getLongitude());
				warnMsg.setWarnUpdateTime(DateUtils.formatDateTimes(busInstance.getTime()));

				logger.error("结束报警{}", JSON.toJSONString(warnMsg));
				warnMsgService.saveWarn(warnMsg);
				busInstance.setWarns(Constants.VAL_BUS_STATUS_7, null);
			}
		}
	}

	@Override
	public void stopWarn(BusInstance busInstance) {
		// TODO Auto-generated method stub
		
	}

}
